﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CRM.Admin.Contracts.Contract.Customer;
using CRM.Admin.Contracts.Dto.Customer;
using KingsSharp.Core;
using KingsSharp.Core.Data;
using CRM.Admin.Contracts.Model.Customer;
using KingsSharp.Common;

namespace CRM.Admin.Services.Service.Customer
{
    public class ProductService : ServiceBase, IProductService
    {
        private IRepository<Product, Guid> productRepository;
        private IRepository<Component, Guid> componentRepository;
        public ProductService(IRepository<Product, Guid> _productRepository, IRepository<Component, Guid> _componentRepository)
            : base(_productRepository.UnitOfWork)
        {
            productRepository = _productRepository;
            componentRepository = _componentRepository;
        }

        public IQueryable<Product> Products
        {
            get { return productRepository.Entities; }
        }

        public IQueryable<Component> Components
        {
            get { return componentRepository.Entities; }
        }

        public KingsSharp.Common.Data.OperationResult Insert(params ProductDto[] dtos)
        {
            dtos.CheckNotNull("dtos");
            var result = productRepository.Insert(dtos);
            return result;
        }

        public KingsSharp.Common.Data.OperationResult Insert(params ComponentDto[] dtos)
        {
            dtos.CheckNotNull("dtos");
            foreach (var t in dtos)
            {
                t.Id = Guid.NewGuid();
            }
            var result = componentRepository.Insert(dtos);
            return result;
        }

        public KingsSharp.Common.Data.OperationResult Update(params ProductDto[] dtos)
        {
            dtos.CheckNotNull("dtos");
            var result = productRepository.Update(dtos);
            return result;
        }

        public KingsSharp.Common.Data.OperationResult Update(params ComponentDto[] dtos)
        {
            dtos.CheckNotNull("dtos");
            var result = componentRepository.Update(dtos);
            return result;
        }

        public KingsSharp.Common.Data.OperationResult Delete(bool isProduct, params Guid[] ids)
        {
            if (isProduct)
            {
                var entitys = productRepository.Entities.Where(a => ids.Contains(a.Id)).ToList();
                entitys.ForEach(a => a.IsDeleted = true);
                int result = productRepository.Update(entitys.ToArray());
                return new KingsSharp.Common.Data.OperationResult(KingsSharp.Common.Data.OperationResultType.Success, "删除成功");
            }
            else
            {
                var entitys = componentRepository.Entities.Where(a => ids.Contains(a.Id)).ToList();
                entitys.ForEach(a => a.IsDeleted = true);
                int result = componentRepository.Update(entitys.ToArray());
                return new KingsSharp.Common.Data.OperationResult(KingsSharp.Common.Data.OperationResultType.Success, "删除成功");
            }
        }
        public KingsSharp.Common.Data.OperationResult Delete( params Guid[] ids)
        {
          
                var entitys = componentRepository.Entities.Where(a => ids.Contains(a.Id)).ToList();
                entitys.ForEach(a => a.IsDeleted = true);
                int result = componentRepository.Update(entitys.ToArray());
                return new KingsSharp.Common.Data.OperationResult(KingsSharp.Common.Data.OperationResultType.Success, "删除成功");
           
               
        }
    }
}
